using System.Collections.Generic;
using EasyPhysic.Collider;
using Unity.Collections;
using UnityEngine;

namespace EasyPhysic.Manager
{
    /// <summary>
    /// 碰撞体集中管理器，由每个业务场景自己负责
    /// </summary>
    public partial class PhysicSystem : IPhysicSystem
    {
        
        /// <summary>
        /// 物理系统
        /// </summary>
        private Dictionary<int, PhysicGroup> _physicGroups = new Dictionary<int, PhysicGroup>();
        /// <summary>
        /// 用于存储四叉树查询结果
        /// </summary>
        private static NativeParallelHashSet<int> results;
        /// <summary>
        /// 战场ID
        /// </summary>
        private int _battleId;
        public int BattleId => _battleId;
        /// <summary>
        /// 需要执行的虚拟碰撞体
        /// </summary>
        public List<ColliderWrapper> Colliders = new List<ColliderWrapper>();
        
        public Dictionary<int,ColliderWrapper> targets = new Dictionary<int,ColliderWrapper>();
      
        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="intervalTimer"></param>
        public void Update(int intervalTimer)
        {
            foreach (var group in _physicGroups.Values)
            {
                // 每个系统更新自己的树
                group.Update(intervalTimer);
            }
            
            //TODO 所有需要检查的碰撞体，会进行碰撞检查，这里需要使用job
            for (int i = Colliders.Count - 1; i >= 0; i--)
            {
                var collider= Colliders[i];
                // TODO 进行合批检查
                // collider.Update(this,intervalTimer);
                if (collider.IsValid)
                {
                    ReferencePool.Release(collider);
                    Colliders.RemoveAt(i);
                }
            }
        }
        public void Init()
        {
            results = new NativeParallelHashSet<int>(20, Allocator.Persistent);
        }

        public void Clear()
        {
            for (int i = Colliders.Count - 1; i >= 0; i--)
            {
                ReferencePool.Release(Colliders[i]);
            }
            targets.Clear();
            Colliders.Clear();
            foreach (var group in _physicGroups.Values)
            {
                group.Clear();
            }
        }

        public void SetBattle(int battleId)
        {
            _battleId = battleId;
        }

        
        public void AppendCollider(ColliderWrapper collider)
        {
            Colliders.Add(collider);
        }
        /// <summary>
        /// 注册碰撞体
        /// </summary>
        /// <param name="collider"></param>
        public void RegisterCollider(ColliderWrapper collider)
        {
            targets.Add(collider.SerialId,collider);
            if (!_physicGroups.ContainsKey(collider.GroupId))
            {
                _physicGroups.Add(collider.GroupId, new PhysicGroup());
            }
            _physicGroups[collider.GroupId].RegisterCollider(collider);
        }

        public void ReleaseCollider(ColliderWrapper collider)
        {
            targets.Remove(collider.SerialId);
            if (_physicGroups.ContainsKey(collider.GroupId))
            {
                _physicGroups[collider.GroupId].ReleaseCollider(collider);
            }
        }

      
       

        public void Release(ColliderWrapper collider)
        {
            Colliders.Remove(collider);
            ReferencePool.Release(collider);
        }

        public void OnDrawGizmos()
        {
            foreach (var collider in Colliders)
            {
                collider.OnDrawGizmos(Color.red);
            }

            foreach (var group in _physicGroups.Values)
            {
                group.OnDrawGizmos(Color.green);
            }
        }
    }
}